<template>
  <el-card shadow="never">
    <template #header>
      <div class="card-header">
        <el-form :inline="true" :model="params.entity" style="padding-bottom: 0px;">
          <el-form-item label="海报ID">
            <el-input v-model="params.entity.id" placeholder="请输入海报ID"/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :icon="Search" @click="searchData">查询</el-button>
            <el-button type="primary" :icon="Plus" @click="addDialog">新增</el-button>
          </el-form-item>
        </el-form>
      </div>
    </template>
    <el-table v-loading="loading"
              :data="tableData.list" style="width: 100%" height="62vh"
              :default-sort="{ prop: 'date', order: 'descending' }"
              @selection-change="handleSelectionChange">
      <!--      <el-table-column fixed type="selection" width="55"/>-->
      <el-table-column prop="id" label="海报ID" show-overflow-tooltip/>
      <el-table-column prop="posterName" label="海报名称" show-overflow-tooltip/>
      <!--      <el-table-column prop="width" label="宽" show-overflow-tooltip/>-->
      <!--      <el-table-column prop="height" label="高" show-overflow-tooltip/>-->
      <el-table-column prop="backgroundUrl" label="背景设置" show-overflow-tooltip>
        <template #default="scope">
          <el-image
              style="width: 50px; height: 50px"
              :src="scope.row.backgroundUrl"
              :zoom-rate="1.2"
              :initial-index="0"
              fit="cover"/>
        </template>
      </el-table-column>
      <el-table-column prop="qrcodeUrl" label="二维码前缀" show-overflow-tooltip/>
      <el-table-column prop="posterTag" label="海报标签" show-overflow-tooltip>
        <template #default="scope">
          <template v-for="tag in scope.row.posterTags">
            &nbsp;
            <el-tag class="ml-2" type="success">{{ tag }}</el-tag>
          </template>
        </template>
      </el-table-column>
      <el-table-column prop="posterDesc" label="海报描述" show-overflow-tooltip/>
      <el-table-column prop="statusFlag" label="是否启用" show-overflow-tooltip>
        <!--        <template #default="scope">-->
        <!--          <el-tag class="ml-2" type="success" v-if="scope.row.statusFlag== 0">启用</el-tag>-->
        <!--          <el-tag class="ml-2" type="danger" v-else>停用</el-tag>-->
        <!--        </template>-->
        <template #default="scope">
          <el-switch
              v-model="scope.row.statusFlagName"
              inline-prompt
              active-text="是"
              inactive-text="否"
              @change="statusFlagChange(scope.row)"
          />
        </template>
      </el-table-column>
      <!--      <el-table-column prop="delFlag" label="是否删除 0-否 1-是" width="160" show-overflow-tooltip/>-->
      <!--      <el-table-column prop="createUser" label="创建人" width="160" show-overflow-tooltip/>-->
      <!--      <el-table-column prop="createTime" label="创建时间" width="160" show-overflow-tooltip/>-->
      <!--      <el-table-column prop="updateTime" label="创建时间" width="160" show-overflow-tooltip/>-->
      <!--      <el-table-column prop="remark" label="备注" width="160" show-overflow-tooltip/>-->

      <el-table-column fixed="right" label="操作" width="300" show-overflow-tooltip>
        <template #header>
          <el-input v-model="params.search" @keyup.enter.native="searchData" placeholder="搜索角色名称/介绍"/>
        </template>
        <template #default="scope">
          <el-button link type="danger" :icon="Delete" size="small" @click="showDeleteDialog(scope.row.id)">删除
          </el-button>
          <el-button link type="primary" :icon="Edit" size="small" @click="editDialog(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination v-model:currentPage="params.current" v-model:page-size="params.size"
                   :page-sizes="[10, 20, 50, 100]" layout="sizes, prev, pager, next" :total="tableData.total"
                   @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
  </el-card>

  <!-- 新增或者修改页面-->
  <BookingSharePosterConfigAddOrEdit
      @closeEditDialog="closeEditDialog"
      :addOrEditVisible="addOrEditVisible"
      :ruleForm="ruleForm"
      :addDataType="addDataType"></BookingSharePosterConfigAddOrEdit>
</template>

<script lang="ts" setup>
import {ElTable, ElMessage, ElMessageBox} from 'element-plus'
import {onMounted, computed, reactive, ref} from 'vue'
import {Params, Result} from "@/apis/model/common/utils.interfaces"
import {Delete, Edit, Search, Share, Upload, Plus, Setting} from '@element-plus/icons-vue'
import BookingSharePosterConfigAddOrEdit from './bookingSharePosterConfigAddOrEdit.vue'
import {BookingSharePosterConfig} from "@/apis/model/booking/BookingSharePosterConfigModel";
import {
  deleteBookingSharePosterConfigById,
  bookingSharePosterConfigPage, updateBookingSharePosterSwitch
} from "@/apis/http/booking/BookingSharePosterConfigHttp";

// 查询
const params: Params = reactive({
  current: 1,
  size: 10,
  search: '',
  entity: {}
})

const ruleForm = ref<BookingSharePosterConfig>({} as BookingSharePosterConfig)

// AddOrEditVisible
const addOrEditVisible = ref(false)

// table 表数据
const tableData: any = ref({});

// 加载数据
const loading = ref(true)
const addDataType = ref('add');

/**
 * 初始化数据
 */
const initList = () => {
  loading.value = true
  bookingSharePosterConfigPage(params).then((res: Result) => {
    const code = res.code
    const message = res.message
    const data = res.data
    if (code == 200) {
      tableData.value = data
    }
    loading.value = false
  })
}

/**
 * search 查询数据
 */
const searchData = () => {
  initList()
}

/**
 * 每页数据量改变查询数据
 * @param val
 */
const handleSizeChange = (val: number) => {
  params.size = val
  initList()
}

/**
 * 分页查询数据
 * @param val
 */
const handleCurrentChange = (val: number) => {
  params.current = val
  initList()
}

/**
 * 添加弹出
 */
const addDialog = () => {
  addOrEditVisible.value = true
  ruleForm.value = {} as BookingSharePosterConfig
  addDataType.value = 'add'
}

//修改弹窗
const editDialog = (row: BookingSharePosterConfig) => {
  addOrEditVisible.value = true
  ruleForm.value = row
  addDataType.value = 'edit'
}

// 关闭修改弹窗
const closeEditDialog = (e: any) => {
  addOrEditVisible.value = e.addOrEditVisible
  if (e.refushList = true) {
    params.current = 1;
    initList();
  }
}

/**
 * 弹出删除对话框
 */
const showDeleteDialog = (id: number) => {
  ElMessageBox.confirm('是否删除?', '提醒', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    deleteBookingSharePosterConfigById(id).then((res: Result) => {
      const code = res.code;
      if (code == 200) {
        ElMessage({
          message: '删除数据成功！',
          type: 'success',
        })
        initList();
      } else {
        ElMessage.error('删除数据失败！');
      }
    })
  }).catch(() => {
  })
}

// 切换状态
const statusFlagChange = (row: BookingSharePosterConfig) => {
  ElMessageBox.confirm(
      '海报配置只能有一个起作用，确定继续?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(() => {
    const param = {id: row.id, statusFlag: row.statusFlagName === true ? 1 : 0} as BookingSharePosterConfig;
    updateBookingSharePosterSwitch(param).then(res => {
      ElMessage({
        type: 'success',
        message: '切换存储类型成功!'
      })
      initList()
    })
  }).catch(() => {
    row.statusFlagName = !row.statusFlagName;
  })
}

onMounted(() => {
  initList()
})
</script>

<style>
</style>
